﻿Imports System
Imports System.Globalization
Imports System.Workflow.Runtime

Namespace System.Workflow.Runtime.Hosting
    Public MustInherit Class WorkflowRuntimeService
        ' Methods
        Protected Sub New()
        End Sub

        Private Sub HandleStarted(ByVal source As Object, ByVal e As WorkflowRuntimeEventArgs)
            Me.state = WorkflowRuntimeServiceState.Started
            Me.OnStarted
        End Sub

        Private Sub HandleStopped(ByVal source As Object, ByVal e As WorkflowRuntimeEventArgs)
            Me.state = WorkflowRuntimeServiceState.Stopped
            Me.OnStopped
        End Sub

        Protected Overridable Sub OnStarted()
        End Sub

        Protected Overridable Sub OnStopped()
        End Sub

        Friend Sub RaiseExceptionNotHandledEvent(ByVal exception As Exception, ByVal instanceId As Guid)
            Me.Runtime.RaiseServicesExceptionNotHandledEvent(exception, instanceId)
        End Sub

        Protected Sub RaiseServicesExceptionNotHandledEvent(ByVal exception As Exception, ByVal instanceId As Guid)
            Me.Runtime.RaiseServicesExceptionNotHandledEvent(exception, instanceId)
        End Sub

        Friend Sub SetRuntime(ByVal runtime As WorkflowRuntime)
            If ((runtime Is Nothing) AndAlso (Not Me._runtime Is Nothing)) Then
                RemoveHandler Me._runtime.Started, New EventHandler([Of] WorkflowRuntimeEventArgs)(AddressOf Me.HandleStarted)
                RemoveHandler Me._runtime.Stopped, New EventHandler([Of] WorkflowRuntimeEventArgs)(AddressOf Me.HandleStopped)
            End If
            Me._runtime = runtime
            If (Not runtime Is Nothing) Then
                AddHandler Me._runtime.Started, New EventHandler([Of] WorkflowRuntimeEventArgs)(AddressOf Me.HandleStarted)
                AddHandler Me._runtime.Stopped, New EventHandler([Of] WorkflowRuntimeEventArgs)(AddressOf Me.HandleStopped)
            End If
        End Sub

        Protected Friend Overridable Sub Start()
            If (Me._runtime Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.ServiceNotAddedToRuntime, New Object() { MyBase.GetType.Name }))
            End If
            If Me.state.Equals(WorkflowRuntimeServiceState.Started) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.ServiceAlreadyStarted, New Object() { MyBase.GetType.Name }))
            End If
            Me.state = WorkflowRuntimeServiceState.Starting
        End Sub

        Protected Friend Overridable Sub [Stop]()
            If (Me._runtime Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.ServiceNotAddedToRuntime, New Object() { MyBase.GetType.Name }))
            End If
            If Me.state.Equals(WorkflowRuntimeServiceState.Stopped) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ExecutionStringManager.ServiceNotStarted, New Object() { MyBase.GetType.Name }))
            End If
            Me.state = WorkflowRuntimeServiceState.Stopping
        End Sub


        ' Properties
        Protected ReadOnly Property Runtime As WorkflowRuntime
            Get
                Return Me._runtime
            End Get
        End Property

        Protected ReadOnly Property State As WorkflowRuntimeServiceState
            Get
                Return Me.state
            End Get
        End Property


        ' Fields
        Private _runtime As WorkflowRuntime
        Private state As WorkflowRuntimeServiceState
    End Class
End Namespace

